package com.decheng.dao.impl;

import com.decheng.dao.UserDao;
import com.decheng.dao.base.BaseDao;
import com.decheng.pojo.User;
import com.decheng.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @Author 孔德成
 * @Slogan 致敬大师，致敬未来的你
 */
public class UserDaoImpl extends BaseDao<User> implements UserDao {

    @Override
    public User getUserByName(User user) {
        Connection conn = null;
        User bean = null;
        try {
            // 1. 获取连接
            conn = JDBCUtil.getConnectionWithDruid();
            // 2. 执行 sql
            String sql = "select * from user where username=?";
            bean = this.getBean(conn, sql, user.getUsername());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //3.  释放资源
            JDBCUtil.closeResource(conn);
        }
        return bean;
    }

    @Override
    public boolean saveUser(User user) {
        Connection conn = null;
        int result = 0;
        try {
            // 1. 获取连接
            conn = JDBCUtil.getConnectionWithDruid();
            // 2. 执行 sql
            String sql = "insert into user values(?,?,?,?)";
            result = this.update(conn, sql, null, user.getUsername(), user.getPassword(), user.getEmail());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //3.  释放资源
            JDBCUtil.closeResource(conn);
        }
        return result > 0;
    }
}
